(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(['HteOS/dock/Dock'],factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	
	/**
	 *  侧边栏管理器
	 *@class HteOS.dock.DockManager
	 *@singleton
	 */
	HteOS.dock.DockManager = {

		/**
		 *@property
		 *用于存放所有侧边栏
		 */
		docks: {},

		/**
		 *初始化侧边栏管理器
		 */
		init: function() {
			function hideDock(e) {
				var target = $(e.target);
				if (target.hasClass("hte-dock")) {
					return;
				}
				if (target.parents(".hte-dock").length > 0) {
					return;
				}

				HteOS.dock.DockManager .hide();
			}
		
			$(document).on('click','.hte-dock-close-btn',function(e){
				var target = e.target;
				dock = HteOS.dock.DockManager.get($(target).parents(".hte-dock").attr("id"));
				dock.hide();
			});
		
			$(document).on('click.hte.dock.data-api', hideDock);

			$(document.body).on("click", "[data-dock=\"true\"]", function(event) {
				var el = $(this),
					dockId = el.data("hte-dock-id"),
					dock;
				if (dockId) {
					//获取侧边栏
					dock = HteOS.dock.DockManager.get(dockId);
				}
				//判断是否已经打开了侧边栏
				if (!dock) {
					//没有打开则新建侧边栏，并向原元素设置侧边栏ID
					dock = new HteOS.dock.Dock({
						id: new Date().getTime(),
						name: el.data("name"),
						path: el.data("path")
					});
					el.data("hte-dock-id", dock.id);
				}
				dock.show();
				//$(this).dock("toggle");
				event.stopPropagation();
			});
			
			HteOS.EventManager.on("hte.dock.beforeshow",this.hide,this);
			HteOS.EventManager.on("hte.dock.show",function(dock){
				HteOS.dock.DockManager.active = dock;
			});
			HteOS.EventManager.on("hte.dock.hide",function(){
				HteOS.dock.DockManager.active = null;
			});
		},

		/**
		 *注册侧边栏
		 */
		register: function(dock) {
			this.docks[dock.id] = dock;
		},

		unregister: function(id) {

		},

		/**
		 *通过ID获取侧边栏
		 */
		get: function(id) {
			return this.docks[id];
		},
		
		clear : function(){
			if(this.active){
				this.active.hide();
			}
		},
		
		hide : function(){
			if(this.active){
				this.active.hide();
			}
		}
	};
}));